package com.lynx.system.controller;

import com.lynx.system.beans.GlobalVO;
import com.lynx.system.beans.Menu;
import com.lynx.system.beans.Order;
import com.lynx.system.beans.User;
import com.lynx.system.feign.OrderFeign;
import com.lynx.system.feign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderFeign orderFeign;
    @GetMapping("redirect/{location}")
    public String redirect(@PathVariable("location") String location){
        return location;
    }
    @GetMapping("/save/{mid}")
    public String save(@PathVariable int mid, HttpSession session){
        User user =(User) session.getAttribute("user");
        Menu menu = new Menu();
        menu.setId(mid);
        Order order = new Order();
        order.setUser(user);
        order.setMenu(menu);
        order.setState(0);
        orderFeign.save(order);
        return "order";
    }
    @GetMapping("/findAllByUid")
    @ResponseBody
    public GlobalVO findAllByUid(@RequestParam int page, @RequestParam int limit, HttpSession session){
        User user = (User) session.getAttribute("user");
        long  uid = user.getId();
        List<Order> orders = orderFeign.findOrdersByUid(uid, (page - 1) * limit, limit);
        int count = orderFeign.findCountByUid(uid);
        GlobalVO result = new GlobalVO(0,"",count,orders);
        return result;
    }
    @GetMapping("/findAllByState")
    @ResponseBody
    public GlobalVO findAllByState(@RequestParam int page,@RequestParam int limit){
        List<Order> orders = orderFeign.findOrdersByState(0, (page - 1) * limit, limit);
        int count = orderFeign.findCountByState(0);
        return new GlobalVO(0,"",count,orders);
    }
    @GetMapping("/updateState/{id}/{state}")
    public String updateState(@PathVariable long id){
        orderFeign.editState(id);
        return "redirect:/order/redirect/order_handler";
    }

}
